# This file will provide a simple LBRY notification system on
# user's system. GPLv3 or later. By J.Y.Amihud.

from getpass import getpass
import requests

auth_token = getpass("Token: ")

# Function to recieve notifications
def fetch_notifications(auth_token):
    notifications = requests.post("https://api.odysee.com/notification/list", data={"auth_token":auth_token}).json()
    if "data" in notifications:
        return notifications["data"]
    else:
        return []

# System Notification Function
def notify(title="LBRY Notification", text=""):

    from subprocess import Popen
    import os
    Popen(["notify-send", "-i", os.getcwd()+"/Odysee.png","-a", "Odysee Notification!", title, text])

    
#Fetching once
notifications = fetch_notifications(auth_token)

for i in notifications:
    if not i["is_read"]:
        print()
        par = i["notification_parameters"]
        if not "comment" in i["type"]:
            notify(par["device"]["title"], par["device"]["name"])
            print(par["device"]["title"], par["device"]["name"])
        else:
            notify(par["device"]["title"], par["dynamic"]["comment"])
            print(par["device"]["title"], par["dynamic"]["comment"])

import time
# Fetch more

while True:
    time.sleep(2) # Wait 2 seconds
    more = fetch_notifications(auth_token)
    for i in more:
        if i not in notifications:
            print()

            par = i["notification_parameters"]
            if not "comment" in i["type"]:
                notify(par["device"]["title"], par["device"]["name"])
                print(par["device"]["title"], par["device"]["name"])
            else:
                notify(par["device"]["title"], par["dynamic"]["comment"])
                print(par["device"]["title"], par["dynamic"]["comment"])
            
            notifications.append(i)
